import pygame
from pygame.locals import *
from sys import exit
import pygame.locals as GAME_GLOBALS
import pygame.event as GAME_EV
import Image
import Main_Window

#重写以便可以返回菜单界面
window = Main_Window.Window()#创建窗口对象
window.CreateWindow()

class Window2:
    def __init__(self):
        self.pywindow2=pygame.display.set_mode((900, 600),0,32)
        self.image=Image.Image()

    def CreateWindow2(self):#开始窗口
        pygame.init()  # 初始化窗口
        pygame.display.set_caption("植物大战僵尸")
        begin = pygame.image.load(self.image.Surface).convert_alpha()
        self.pywindow2.blit(begin, (0, 0))
        begin1 = pygame.image.load(self.image.PlayGame1).convert_alpha()#开始冒险吧图片
        self.pywindow2.blit(begin1, (470, -50))
        #ZX添加
        begin2 = pygame.image.load(self.image.zwjs).convert_alpha()
        self.pywindow2.blit(begin2,(470,210))
        #抽奖按钮
        begin3 = pygame.image.load(self.image.cj).convert_alpha()
        self.pywindow2.blit(begin3,(350,450))
        pygame.display.update()

    def ZWTJ(self):
        # 初始化
        pygame.init()

        # 设置窗口大小
        size = width, height = 900, 600
        screen = pygame.display.set_mode(size)

        # 窗口名
        pygame.display.set_caption("植物图鉴")

        # 导入背景图片
        BLACK = (0, 0, 0)
        background_image = pygame.image.load('jspvz/main_image/zwjs/background.png').convert()

        # 计算缩放比例并缩放图片以适应窗口大小
        background_rect = background_image.get_rect()
        scale_width = size[0]
        scale_height = int(background_rect.height * (scale_width / float(background_rect.width)))
        scaled_background = pygame.transform.scale(background_image, (scale_width, scale_height))

        # 确保背景图片在垂直方向上居中
        background_y = (height - scaled_background.get_height()) // 2
        background_x = 0
        back_image=pygame.image.load('jspvz/main_image/zwjs/back.png').convert_alpha()

        # 导入卡片
        card_image_paths = [
            'jspvz/main_image/zwjs/card_image_path/card_sunflower.png',
            'jspvz/main_image/zwjs/card_image_path/card_wallnut.png',
            'jspvz/main_image/zwjs/card_image_path/card_peashooter.png',
            'jspvz/main_image/zwjs/card_image_path/card_cherrybomb.png',
            'jspvz/main_image/zwjs/card_image_path/card_squash.png',
            'jspvz/main_image/zwjs/card_image_path/card_chomper.png',
            'jspvz/main_image/zwjs/card_image_path/card_doomshroom.png',
            'jspvz/main_image/zwjs/card_image_path/card_gravebuster.png',
            'jspvz/main_image/zwjs/card_image_path/card_hypnoshroom.png',
            'jspvz/main_image/zwjs/card_image_path/card_iceshroom.png',
            'jspvz/main_image/zwjs/card_image_path/card_potatomine.png',
            'jspvz/main_image/zwjs/card_image_path/card_puffshroom.png',
            'jspvz/main_image/zwjs/card_image_path/card_snowpea.png',
            'jspvz/main_image/zwjs/card_image_path/card_sunshroom.png',
            'jspvz/main_image/zwjs/card_image_path/card_torchwood.png',
        ]
        images = []

        # 压缩卡片，放入对应位置
        for path in card_image_paths:
            image = pygame.image.load(path).convert_alpha()
            scaled_w = 81
            scaled_h = 105
            scaled_image = pygame.transform.scale(image, (scaled_w, scaled_h))
            images.append(scaled_image)
        card_positions = [
            (37, 163),
            (136, 163),
            (234, 163),
            (330, 163),
            (429, 163),
            (37, 284),
            (136, 284),
            (234, 284),
            (330, 284),
            (429, 284),
            (37, 403),
            (136, 403),
            (234, 403),
            (330, 403),
            (429, 403),
        ]

        # 卡片对应介绍
        card_to_new_image = {
            0: 'jspvz/main_image/zwjs/card_to_new_image/sunflower.png',
            1: 'jspvz/main_image/zwjs/card_to_new_image/wallnut.png',
            2: 'jspvz/main_image/zwjs/card_to_new_image/peashooter.png',
            3: 'jspvz/main_image/zwjs/card_to_new_image/cherrybomb.png',
            4: 'jspvz/main_image/zwjs/card_to_new_image/squash.png',
            5: 'jspvz/main_image/zwjs/card_to_new_image/chomper.png',
            6: 'jspvz/main_image/zwjs/card_to_new_image/doomshroom.png',
            7: 'jspvz/main_image/zwjs/card_to_new_image/gravebuster.png',
            8: 'jspvz/main_image/zwjs/card_to_new_image/hypnoshroom.png',
            9: 'jspvz/main_image/zwjs/card_to_new_image/iceshroom.png',
            10: 'jspvz/main_image/zwjs/card_to_new_image/peashooter.png',
            11: 'jspvz/main_image/zwjs/card_to_new_image/potatomine.png',
            12: 'jspvz/main_image/zwjs/card_to_new_image/puffshroom.png',
            13: 'jspvz/main_image/zwjs/card_to_new_image/snowpea.png',
            14: 'jspvz/main_image/zwjs/card_to_new_image/sunshroom.png',
            15: 'jspvz/main_image/zwjs/card_to_new_image/torchwood.png',
        }

        # 初始化当前显示的新图片为None
        current_new_image = None
        new_image_position = (536, 164)

        def is_rect(self, pos, rect):  # 计算图片的点击区域(返回按钮)
            x, y = pos  # 点击的位置
            rx, ry, rw, rh = rect
            if (rx <= x <= rx + rw) and (ry <= y <= ry + rh):
                return True
            return False

        # 事件处理
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit()
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    #返回
                    x1,y1=event.pos
                    if 15<=x1<=170 and 510<=y1<=540:
                        w,h=pygame.image.load('jspvz/main_image/zwjs/back.png').convert_alpha().get_size()
                        if window.is_rect(event.pos,(15,510,w,h/2)):
                            window.CreateWindow()
                            return

                    # 点击卡片图片时加载并显示对应的新图片
                    for i, (image, pos) in enumerate(zip(images, card_positions)):
                        rect = image.get_rect(topleft=pos)
                        if rect.collidepoint(pygame.mouse.get_pos()):
                            # 根据点击的卡片加载新图片
                            new_image_path = card_to_new_image[i]
                            current_new_image = pygame.image.load(new_image_path).convert_alpha()


            screen.fill(BLACK)
            screen.blit(scaled_background, (background_x, background_y))

            # 绘制卡片图片并根据鼠标位置进行缩放
            zoom_factor = 1.12
            for i, (image, pos) in enumerate(zip(images, card_positions)):
                rect = image.get_rect(topleft=pos)
                if rect.collidepoint(pygame.mouse.get_pos()):
                    scaled_image = pygame.transform.scale(image,
                                                          (int(scaled_w * zoom_factor), int(scaled_h * zoom_factor)))
                else:
                    scaled_image = image
                screen.blit(scaled_image, pos)

            # 绘制介绍图片
            if current_new_image is not None:
                screen.blit(current_new_image, new_image_position)
            screen.blit(back_image,(15,510))
            pygame.display.flip()
            pygame.time.Clock().tick(60)



